<template>
  <div>
    <el-table ref="filterTable" :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="120">
      </el-table-column>
      <el-table-column prop="account" label="账号名称" width="180">
      </el-table-column>
      <el-table-column prop="payMentMethod.method" label="支付类型" width="180">
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
      </el-table-column>
      <el-table-column prop="status" label="标签" width="100"
        :filters="[{ text: '启用', value: 'y' }, { text: '禁用', value: 'n' }]" :filter-method="filterTag"
        filter-placement="bottom-end">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 'n' ? 'primary' : 'success'" disable-transitions
            v-if="scope.row.status == 'y'">启用
          </el-tag>
          <el-tag :type="scope.row.status === 'n' ? 'primary' : 'success'" disable-transitions v-else>禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot="header">
          <strong>操作</strong>
          <el-button size="mini" type="success" style="margin-left:30px" @click="initAdd()">增加</el-button>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="primary " @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleted(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-dialog title="修改收款账号" :visible.sync="dialogFormVisible">
        <el-form :model="chargeAccount">
          <el-form-item label="账号名称" :label-width="formLabelWidth">
            <el-input v-model="chargeAccount.account" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="支付类型" :label-width="formLabelWidth">
            <el-select v-model="chargeAccount.payMentMethod.id" placeholder="请选择支付类型">
              <el-option v-for="payMethod in payMehthods" :key="payMethod.id" :label="payMethod.method" :value="payMethod.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账号状态" :label-width="formLabelWidth">
            <el-select v-model="chargeAccount.status" placeholder="请选择账号状态">
              <el-option label="启用" value="y"></el-option>
              <el-option label="禁用" value="n"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" :plain="true" @click="updatedAccount()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <el-dialog title="增加收款账号" :visible.sync="dialogAddFormVisible">
        <el-form :model="chargeAccount">
          <el-form-item label="账号名称" :label-width="formLabelWidth">
            <el-input v-model="chargeAccount.account" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="支付类型" :label-width="formLabelWidth">
            <el-select v-model="chargeAccount.payMentMethod.id" placeholder="请选择支付类型">
              <el-option v-for="payMethod in payMehthods" :key="payMethod.id" :label="payMethod.method" :value="payMethod.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账号状态" :label-width="formLabelWidth">
            <el-select v-model="chargeAccount.status" placeholder="请选择账号状态">
              <el-option label="启用" value="y"></el-option>
              <el-option label="禁用" value="n"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAddFormVisible = false">取 消</el-button>
          <el-button type="primary" :plain="true" @click="add()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  name: "ChargeConfig",
  data() {
    return {
      page: 1,
      value: '',
      tableData: [],
      payMehthods:[],
      dialogFormVisible: false,
      dialogAddFormVisible: false,
      formLabelWidth: '120px',
      chargeAccount: {
        id: "",
        account: "",
        status: "",
        payMentMethod: {
          id: "",
          method: "",
        },
      },
    }
  },
  methods: {
    filterTag(value, row) {
      return row.status === value;
    },
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.getById(row.id);
      this.getPayMethod();
    },
    handleDelete(index, row) {
      this.$axios.post("http://127.0.0.1:8080/api/v1/config/chargeAccount/delete/" + row.id).then(r => {
        this.getChargeAccounts();
      })
    },
    initAdd() {
      this.dialogAddFormVisible = true;
      this.getPayMethod();
    },
    deleted(index, row) {
      if (row.status == "y") {
        this.$message({
          showClose: true,
          message: '本条数据是启用状态，禁止删除',
          type: 'error'
        });
      } else {
        this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(index, row);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },
    getChargeAccounts() {
      this.$axios.post("http://127.0.0.1:8080/api/v1/config/chargeAccount/list").then(r => {
        let q = JSON.parse(JSON.stringify(r));
        // 解决[__ob__: Observer]问题
        console.log(q);
        if (q.data.code == 200) {
          console.log(q.data.code);
          console.log(q.data.data);
          this.tableData = q.data.data;
        }
      })
    },
    getById(id) {
      this.$axios.post("http://127.0.0.1:8080/api/v1/config/chargeAccount/getById/" + id).then(r => {
        if (r.data.code == 200) {
          console.log(r);
          this.chargeAccount = r.data.data;

        }
      })
    },
    updatedAccount() {
      console.log(this.chargeAccount.payMentMethod.id)
      this.$axios.post("http://127.0.0.1:8080/api/v1/config/chargeAccount/update", this.chargeAccount).then(r => {
        if (r.data.code == 200) {
          this.dialogFormVisible = false;
          this.getChargeAccounts();
          this.$message({
            message: '修改成功',
            type: 'success'
          });
        }
      })
    },
    add() {
      this.$axios.post("http://127.0.0.1:8080/api/v1/config/chargeAccount/add", this.chargeAccount).then(r => {
        if (r.data.code == 200) {
          this.dialogAddFormVisible = false;
          this.getChargeAccounts();
          this.$message({
            message: '增加成功',
            type: 'success'
          });
        }
      })
    },
     getPayMethod(){
      this.$axios.post("http://127.0.0.1:8080/api/v1/pay/getPayMMList").then(r => {
        if (r.data.code == 200) {
          console.log(r);
          this.payMehthods=r.data.data;
        }
      })
    }
  },
  created() {
    this.getChargeAccounts();
  }
}
</script>